package com.example.demo;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.view.Window;
import android.webkit.JavascriptInterface;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.example.demo.base.AppInfo;
import com.example.demo.print.PrintUtil;
import com.example.demo.util.ToastUtil;
import com.google.gson.Gson;

public class MainActivity extends AppCompatActivity {

    private final static String TAG = "MainActivity";
    private final static boolean DBU = true;

    private final static String HTTP_URL = "http://125.35.221.187:8009/diancan";
    private final static String CALL_NAME = "bt";

    private WebView mWebView;

    BluetoothAdapter mAdapter;
    boolean mBtEnable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        initWebView();
    }

    @Override
    protected void onStart() {
        super.onStart();
        BluetoothController.init(this);
    }

    @SuppressLint("SetJavaScriptEnabled")
    private void initWebView() {
        mWebView = findViewById(R.id.webView);
        mWebView.getSettings().setJavaScriptEnabled(true);//支持js

        mWebView.loadUrl(HTTP_URL);

        //设置不用系统浏览器打开,直接显示在当前Webview
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                view.loadUrl(request.getUrl().toString());
                return true;
            }
        });

        // 通过addJavascriptInterface()将Java对象映射到JS对象
        //参数1：Javascript对象名
        //参数2：Java对象名
        mWebView.addJavascriptInterface(new Android2Js(this), CALL_NAME);//AndroidtoJS类对象映射到js的test对象
    }

    //点击返回上一页面而不是退出浏览器
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    //销毁Webview
    @Override
    protected void onDestroy() {
        if (mWebView != null) {
            mWebView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
            mWebView.clearHistory();

            ((ViewGroup) mWebView.getParent()).removeView(mWebView);
            mWebView.destroy();
            mWebView = null;
        }
        super.onDestroy();
    }

    class Android2Js {

        private Context mContext;

        Android2Js (Context context) {
            mContext = context;
        }

        // 定义JS需要调用的方法
        // 被JS调用的方法必须加入@JavascriptInterface注解
        @JavascriptInterface
        public void print(String msg) {
            Log.d(TAG, "Android2Js print");
            btPrint(msg);
        }
    }

    private void btPrint(String msg) {
        if (android.text.TextUtils.isEmpty(AppInfo.btAddress)) {
            ToastUtil.showToast(this,"请连接蓝牙...");
            startActivity(new Intent(MainActivity.this, SearchBluetoothActivity.class));
        }else {
            if (mAdapter.getState() == BluetoothAdapter.STATE_OFF){//蓝牙被关闭时强制打开
                mAdapter.enable();
                ToastUtil.showToast(this,"蓝牙被关闭请打开...");
            } else {
                ToastUtil.showToast(this,"正在打印");
                Intent intent = new Intent(getApplicationContext(), BtService.class);
                intent.setAction(PrintUtil.ACTION_PRINT_TICKET);
                intent.putExtra(PrintUtil.PRINT_EXTRA, msg);
                startService(intent);
            }
        }
    }
}
